# -*- coding:utf-8 -*-
# @Time : 21-7-29 上午11:06 
# @Author : zdy
# @File : StandardMode.py
# desc:

from Class.VoltSignal import *
from Class.CSConfig import BaseCalibrationItem, MultiPointCalibrationItem


class ResultMode(QStandardItemModel):
    def __init__(self, parent=None):
        super().__init__()
        #TODO
        # QStandardItemModel.__init__(self, parent)
        self.setColumnCount(dataBase.colNum)
        self.setHorizontalHeaderLabels(dataBase.dbColNames)

    # def data(self, index, role=None):  # real signature unknown; restored from __doc__
    #     """ data(self, QModelIndex, role: int = Qt.DisplayRole) -> Any """
    #     value = self.data(index, role)
    #     if Qt.TextAlignmentRole == role:
    #         value = Qt.AlignCenter
    #     return value

    def getBaseItems(self, rowIndexs):
        '''
        用于基础校正功能，根据行号返回BaseItenms
        :param rowIndexs: 选中行的行号
        :return: List[BaseItenms]
        '''
        results = []
        for index in rowIndexs:
            row = index
            item = BaseCalibrationItem()
            try:
                item.id = self.item(row, dataBase.dbColId[1]).text()
                item.weight = float(self.item(row, dataBase.dbColSampleWeight[1]).text())
                item.num = int(self.item(row, dataBase.dbColSampleNum[1]).text())
                item.name = self.item(row, dataBase.dbColSampleName[1]).text()
                results.append(item)
            except:
                print("数据格式错误")
        return results

    def getMultiPointItems(self, rowIndexs):
        '''
        用于基础校正功能，根据行号返回BaseItenms
        :param rowIndexs: 选中行的行号
        :return: List[MultiPointCalibrationItems]
        '''
        results = []
        for index in rowIndexs:
            row = index
            item = MultiPointCalibrationItem()
            try:
                item.id = self.item(row, dataBase.dbColId[1]).text()
                item.weight = float(self.item(row, dataBase.dbColSampleWeight[1]).text())
                item.num = int(self.item(row, dataBase.dbColSampleNum[1]).text())
                item.name = self.item(row, dataBase.dbColSampleName[1]).text()
                results.append(item)
            except:
                print("数据格式错误")
        return results

    def removeRowsByIndex(self, modelIndexs):
        '''
        根据行序号，删除数据
        :param modelIndexs: QModelIndex
        :return:
        '''
        list = []  # 创建一个空list用于存放需要删除的行号
        for index in modelIndexs:
            list.append(index.row())  # 获得需要删除的行号的list
        list.sort(key=int, reverse=True)  # 因为删除一行后，后面的行号都会变化，所以用sort方法将list进行降序排列
        for i in list:  # 按照list删除对应行
            self.removeRow(i)

    def addRow(self, model, indexs):
        """
        将model中的rowIndexs行加载到当前model中
        :param model: 数据源
        :param rowIndexs: 待插入行的QmodelIndex
        :return:
        """
        # 新增行
        self.beginResetModel()
        rows = self.rowCount()
        addrows =len(indexs)
        self.beginInsertRows(QModelIndex(), rows, rows+addrows)
        self.endInsertRows()

        for i in range(addrows):
            for j in range(0, dataBase.colNum):
                item = QStandardItem()
                value = model.item(indexs[i].row(), j).data(0)
                item.setData(value, Qt.DisplayRole)
                item.setTextAlignment(Qt.AlignHCenter)
                self.setItem(rows+i, j, item)
    def addRow2(self, model, indexs):
        """
        将model中的rowIndexs行加载到当前model中
        :param model: 数据源
        :param rowIndexs: 待插入行的QmodelIndex
        :return:
        """
        # 新增行
        self.beginResetModel()
        rows = self.rowCount()
        addrows =len(indexs)
        self.beginInsertRows(QModelIndex(), rows, rows+addrows)
        self.endInsertRows()

        for i in range(addrows):
            for j in range(0, dataBase.colNum):
                item = QStandardItem()
                value = model.item(indexs[i], j).data(0)
                item.setData(value, Qt.DisplayRole)
                item.setTextAlignment(Qt.AlignHCenter)
                self.setItem(rows+i, j, item)
class BaseMultiMode(QStandardItemModel):
    def __init__(self):
        super().__init__()
        self.setColumnCount(dataBase.colNum)
        self.setHeaderData(0, Qt.Horizontal, "时间")
        self.setHeaderData(1, Qt.Horizontal, "样品名称")
        self.setHeaderData(2, Qt.Horizontal, "编号")
        self.setHeaderData(3, Qt.Horizontal, "样品质量")
        self.setHeaderData(4, Qt.Horizontal, "助熔剂质量")
        self.setHeaderData(5, Qt.Horizontal, "空白")
        self.setHeaderData(6, Qt.Horizontal, "面积")
        self.setHeaderData(7, Qt.Horizontal, "标准值")
        self.setHeaderData(8, Qt.Horizontal, "包含")
        self.setHeaderData(9, Qt.Horizontal, "输入")
        self.setHeaderData(10, Qt.Horizontal, "计算后")



